/*
 * YuebaoBuffer.cpp
 *
 *  Created on: 2011-3-19
 *      Author: wangchao
 */

#include "YuebaoBuffer.h"

YuebaoBufferBase::YuebaoBufferBase( int length ) :
    _bufferLength( length ), _bufferPtr( 0 )
{

}
YuebaoBufferBase::~YuebaoBufferBase( )
{
    delete _bufferPtr;
    _bufferPtr = NULL;
}

void YuebaoBufferBase::SetMaxLength( int length )
{
    if( length < 1 )
    {
        return;
    }
    if( _bufferPtr )
    {
        if( _bufferPtr->Des( ).MaxLength( ) < length )
        {
            _bufferPtr = _bufferPtr->ReAlloc( length );
            _bufferLength = length;
        }
    }
    else
    {
        _bufferLength = length;
        _bufferPtr = HBufC::NewL( _bufferLength );
    }
}

HBufC* YuebaoBufferBase::Buffer( )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }
    return _bufferPtr;
}

const TDesC& YuebaoBufferBase::operator()( ) const
{
    if( !_bufferPtr )
    {
       return KNullDesC;
    }
    return *_bufferPtr;
}

TPtr YuebaoBufferBase::Des( )
{
    return Buffer( )->Des( );
}
int YuebaoBufferBase::Length( )
{
    if( !_bufferPtr )
    {
        return 0;
    }
    else
    {
        return _bufferPtr->Length( );
    }
}
void YuebaoBufferBase::Zero( )
{
    if( _bufferPtr )
    {
        _bufferPtr->Des( ).Zero( );
    }
}
void YuebaoBufferBase::Copy( const TDesC8& aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }
    int appendLength = aDes.Length( );
    if( appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( appendLength );
    }

    _bufferPtr->Des( ).Copy( aDes );
}
void YuebaoBufferBase::Copy( const TDesC16& aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }
    int appendLength = aDes.Length( );
    if( appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( appendLength );
    }

    _bufferPtr->Des( ).Copy( aDes );
}
void YuebaoBufferBase::Append( TChar ch )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }

    int appendLength = 1;
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    _bufferPtr->Des( ).Append( ch );
}

void YuebaoBufferBase::Append( const TDesC16 &aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }

    int appendLength = aDes.Length( );
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    _bufferPtr->Des( ).Append( aDes );
}

void YuebaoBufferBase::AppendFormat( TRefByValue< const TDesC > aFmt, ... )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }

    int appendLength = ( ( const TDesC& )aFmt ).Length( ) + 128;
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    VA_LIST ap;
    VA_START(ap,aFmt);
    _bufferPtr->Des( ).AppendFormatList( aFmt, ap );
    VA_END(ap);
}
void YuebaoBufferBase::AppendNum( TInt64 number )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }

    int appendLength = 32;
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }
    _bufferPtr->Des( ).AppendNum( number );
}

void YuebaoBufferBase::Format( TRefByValue< const TDesC > aFmt, ... )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC::NewL( _bufferLength );
    }
    else
    {

    }

    VA_LIST ap;
    VA_START(ap,aFmt);
    _bufferPtr->Des( ).FormatList( aFmt, ap );
    VA_END(ap);
}

int YuebaoBufferBase::Compare( const TDesC& aDes )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Compare( aDes );
    }
    return -1;
}

int YuebaoBufferBase::Find( const TDesC& aDes )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Find( aDes );
    }
    return -1;
}
int YuebaoBufferBase::Locate( TChar ch )
{
    if( _bufferPtr )
    {
        return _bufferPtr->LocateReverse( ch );
    }
    return -1;
}
int YuebaoBufferBase::LocateReverse( TChar ch )
{
    if( _bufferPtr )
    {
        return _bufferPtr->LocateReverse( ch );
    }
    return -1;
}
TPtrC YuebaoBufferBase::Left( int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Left( length );
    }
    else
    {

        return KNullDesC( );//TPtrC;
    }
}

TPtrC YuebaoBufferBase::Right( int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Right( length );
    }
    else
    {

        return KNullDesC( );//TPtrC;
    }
}
TPtrC YuebaoBufferBase::Mid( int pos )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Mid( pos );
    }
    else
    {

        return KNullDesC( );//TPtrC;
    }
}
TPtrC YuebaoBufferBase::Mid( int pos, int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Mid( pos, length );
    }
    else
    {

        return KNullDesC( );//TPtrC;
    }
}

void YuebaoBufferBase::LowerCase( )
{
    if( _bufferPtr )
    {
        _bufferPtr->Des( ).LowerCase( );
    }
}

void YuebaoBufferBase::UpperCase( )
{
    if( _bufferPtr )
    {
        _bufferPtr->Des( ).UpperCase( );
    }
}
